package com.jifeng.br.pjj.bookreader.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.br.pjj.bookreader.entity.AdEntity;
import com.jifeng.br.pjj.bookreader.mapper.AdMapper;
import com.jifeng.br.pjj.bookreader.service.AdService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 广告表 服务实现类
 * </p>
 *
 * @author 机蜂
 * @since 2023-07-11
 */
@Service
@Transactional
public class AdServiceImpl extends ServiceImpl<AdMapper, AdEntity> implements AdService {

    @Override
    public IPage<AdEntity> getPage(int page, int limit, AdEntity ad) {
        return this.page(new Page<>(page, limit), new QueryWrapper<AdEntity>()
                .eq(StringUtils.isNotEmpty(ad.getName()), "name", ad.getName())
                .eq(ad.getStatus() != null, "status", ad.getStatus()).lt(StringUtils.isNotEmpty(ad.getSearchExpires()), "expires_start", ad.getSearchExpires())
                .gt(StringUtils.isNotEmpty(ad.getSearchExpires()), "expires_end", ad.getSearchExpires())
                .orderByDesc(ad.getOrderNo() != null, "order_no", "id")
        );
    }

    @Override
    public List<AdEntity> getList() {
        return this.list(new QueryWrapper<AdEntity>()
                .eq("status", 1)
                .lt("expires_start", new Date())
                .gt("expires_end", new Date())
                .orderByDesc("order_no")
        );
    }
}
